const Sequelize = require("sequelize");
const seq = require("./seq");

// 创建user模型
const User = seq.define("user", {
  // id 自增
  userName: {
    type: Sequelize.STRING, // 使用了Sequelize的数据类型 varchar(255)
    allowNull: false, // 不允许为空
    comment: "用户名",
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: "密码",
  },
  nickname: {
    type: Sequelize.STRING,
    comment: "昵称",
  },
});

const Blog = seq.define("blog", {
  title: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: "标题",
  },
  content: {
    type: Sequelize.TEXT,
    allowNull: false,
    comment: "微博内容",
  },
  image: {
    type: Sequelize.STRING,
    comment: "图片地址",
  },
});

// 外键关联
Blog.belongsTo(User, {
  foreignKey: "userId", // blog的userId是关联了user的id的外键
});
User.hasMany(Blog, {
  foreignKey: "userId",
});

module.exports = {
  User,
  Blog,
};
